package com.hncboy.chatgpt.front.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hncboy.chatgpt.base.cache.CodeCache;
import com.hncboy.chatgpt.base.domain.entity.ChatOrderDO;
import com.hncboy.chatgpt.base.mapper.ChatCustomerMapper;
import com.hncboy.chatgpt.base.util.DateUtil;
import com.hncboy.chatgpt.front.domain.vo.ChatOrderVO;
import com.hncboy.chatgpt.front.mapper.ChatOrderMapper;
import com.hncboy.chatgpt.front.service.ChatOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author hncboy
 * @date 2023/3/22 20:05
 * 鉴权相关业务实现类
 */
@Slf4j
@Service
public class ChatOrderServiceImpl
        extends ServiceImpl<ChatOrderMapper,ChatOrderDO>
        implements ChatOrderService {

    @Resource
    private ChatCustomerMapper chatCustomerMapper;
    @Resource
    private ChatOrderMapper chatOrderMapper;
    @Resource
    private CodeCache codeCache;

    private List<ChatOrderDO> getOrder(Long customerId){
        QueryWrapper<ChatOrderDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId);
        return super.list(queryWrapper);
    }


    @Override
    public int createOrder(ChatOrderVO chatOrderVO) {
        ChatOrderDO chatOrderDO = new ChatOrderDO();
        BeanUtil.copyProperties(chatOrderVO, chatOrderDO);
        QueryWrapper<ChatOrderDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", chatOrderVO.getCustomerId());
        List<ChatOrderDO> chatCustomerDOS = chatOrderMapper.selectList(queryWrapper);
        if (CollUtil.isEmpty(chatCustomerDOS)) {
            chatOrderDO.setStatus(0);
            chatOrderDO.setSort(0);
        }else{
            chatOrderDO.setSort(chatCustomerDOS.size());
        }
        return chatOrderMapper.insert(chatOrderDO);
    }

    @Override
    public int modifyOrder(Long id) {
        ChatOrderDO chatOrder = chatOrderMapper.selectById(id);
        Integer status = chatOrder.getStatus();
        if (status != 0) {
            throw new RuntimeException("订单状态有误");
        }
        chatOrder.setBegin(new Date());
        Integer type = chatOrder.getType();
        Integer level = chatOrder.getLevel();
        if (1 == type) {
            switch (level) {
                case 1:
                    chatOrder.setEnd(DateUtil.offsetDay(new Date(),1));
                    break;
                case 2:
                    chatOrder.setEnd(DateUtil.offsetDay(new Date(),7));
                    break;
                case 3:
                    chatOrder.setEnd(DateUtil.offsetMonth(new Date(),1));
                    break;
                case 4:
                    chatOrder.setEnd(DateUtil.offsetMonth(new Date(),3));
                    break;
                case 5:
                    chatOrder.setEnd(DateUtil.offsetMonth(new Date(),12));
                    break;
            }
        }
        return chatOrderMapper.updateById(chatOrder);
    }



    @Override
    public ChatOrderVO closeOrder(List<Integer> ids) {
        ids.stream().forEach(id -> {
            ChatOrderDO chatOrderDO = new ChatOrderDO();
            chatOrderDO.setStatus(2);
            chatOrderMapper.updateById(chatOrderDO);
        });
        return null;
    }

}
